package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.dto.CustomersDto;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.model.ChannelModel;
import com.bridgeintelligent.tag.customer.model.ChannelQueryModel;
import com.bridgeintelligent.tag.customer.service.IChannelService;
import com.bridgeintelligent.tag.customer.vo.ChannelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @description
 * @author: grp 2021-02-21 22:56
 **/
@Api(tags = "渠道管理")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/channel")
public class ChannelController {

    IChannelService channelService;

    @ApiOperation(value = "新增渠道", notes = "渠道管理")
    @PostMapping("add")
    public CustomersDto<Object> add(@RequestBody @Valid ChannelModel model) {
        int count = channelService.countById(model.getChannelId());
        if(count>0){
            return CustomersDto.error("渠道id不能重复！");
        }
        count = channelService.countByName(model.getChannelName());
        if(count>0){
            return CustomersDto.error("渠道名称重复，请修改渠道名称后重新提交！");
        }
        channelService.add(model);
        return CustomersDto.success();
    }
    
    @ApiOperation(value = "修改渠道", notes = "渠道管理")
    @PostMapping("put")
    public CustomersDto<Object> put(@RequestBody @Valid ChannelModel model) {
        if(StringUtils.isBlank(model.getChannelId())){
            return CustomersDto.error("渠道id不能为空！");
        }
        int count = channelService.countByNameAndNotId(model.getChannelId(),model.getChannelName());
        if(count>0){
            return CustomersDto.error("渠道名称重复，请修改渠道名称后重新提交！");
        }
        channelService.put(model);
        return CustomersDto.success();
    }

    @ApiOperation(value = "渠道列表", notes = "渠道管理")
    @GetMapping("list")
    public CustomersDto<CustomersPage<ChannelVO>> list(@ModelAttribute @Valid ChannelQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        CustomersPage<ChannelVO> page = channelService.list(model);
        return CustomersDto.success(page);
    }

    @ApiOperation(value = "渠道删除", notes = "渠道管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "渠道ID", required = true)})
    @DeleteMapping("del")
    public CustomersDto<Object> del(@NotBlank String id) {
        channelService.del(id);
        return CustomersDto.success();
    }

    @ApiOperation(value = "渠道详情", notes = "渠道管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id", required = true)})
    @GetMapping("detail")
    public CustomersDto<ChannelVO> detail(@NotBlank String id) {
        return CustomersDto.success(channelService.getDetail(id));
    }

    @ApiOperation(value = "渠道列表(下发)", notes = "渠道管理")
    @GetMapping("channels")
    public CustomersDto<List<ChannelVO>> channels() {
        List<ChannelVO> list = channelService.channels();
        return CustomersDto.success(list);
    }

    @ApiOperation(value = "渠道列表(全部)", notes = "渠道管理")
    @GetMapping("allChannels")
    public CustomersDto<List<ChannelVO>> allChannels() {
        List<ChannelVO> list = channelService.allChannels();
        return CustomersDto.success(list);
    }
    
}
